#!/bin/sh
#add by banzhenyu

#常量
aapt_for_linux=./utils/xbin/aapt_19.0.1_linux
aapt_for_mac=./utils/xbin/aapt_23.0.1_mac
apks_path=./public/apks
appName=updater

#export NODE_ENV=development
export NODE_ENV=production

echo "拉取最新代码"
git pull origin

#安装包管理器m)
echo "check cnpm :"
which cnpm >/dev/null
if [ $? -gt 0 ]
then
    echo "start install cnpm"
    npm install -g cnpm --registry=https://registry.npm.taobao.org
fi
cnpmVersion=`cnpm -v`
echo "cnpm version: ${cnpmVersion}"


echo "check pm2 : "
which pm2 >/dev/null
if [ $? -gt 0 ]
then
    echo "start install pm2"
    cnpm install pm2 -g
fi
pm2Version=`pm2 -v`
echo "pm2 version: ${pm2Version}"

#安装依赖
echo "安装中间件"
cnpm install --save

echo "初始化安装包保存目录"
#检查保存安卓安装包文件的文件夹是否存在
if test -e ${apks_path}
then
    echo 'apksdir is exist!'
else
    echo 'apksdir not found ====> create dir!'
    cd ./public
    mkdir apks
fi

echo "初始化Android Asset Packaging Tool"
if test -e ${aapt_for_linux}
then
    echo 'Change mod a+x aapt for linux!'
    chmod a+x ${aapt_for_linux}
else
    echo 'aapt for linux not found!'
fi

if test -e ${aapt_for_mac}
then
    echo 'Change mod a+x aapt for macosx!'
    chmod a+x ${aapt_for_mac}
else
    echo 'aapt for macosx not found!'
fi

echo "开启Node进程"
pm2 show ${appName} >/dev/null
if [ $? -gt 0 ]
then
    pm2 start ./bin/${appName}
else
    echo "process starting"
    pm2 restart all
fi
